// Created by zengyuhao on 25-8-26.
// Copyright (c) 2025 Engineai All rights reserved.
//

#ifndef UTILITIES_FUNCTION_H
#define UTILITIES_FUNCTION_H

#include <cmath>
#include <algorithm>

#include <math.h>
#include <vector>

bool IsClose(const std::vector<double> &a, const std::vector<double> &b, const double &tolerance);

// Clamp a value between [lo, hi]. Uses std::clamp (C++17).
inline double Clamp(const double v, const double lo, const double hi)
{
	return std::clamp(v, lo, hi);
}

// Linearly map a value from [in_lo, in_hi] to [out_lo, out_hi].
// If saturate_input is true, x will be clamped to [in_lo, in_hi] before mapping.
inline double LinearMap(double x,
						const double in_lo,
						const double in_hi,
						const double out_lo,
						const double out_hi,
						const bool saturate_input = false)
{
	if (saturate_input)
	{
		x = Clamp(x, in_lo, in_hi);
	}
	const double denom = (in_hi - in_lo);
	if (denom == 0.0)
	{
		// Degenerate case: avoid division by zero; return lower bound of output.
		return out_lo;
	}
	const double t = (x - in_lo) / denom; // normalized [0,1] (if saturated)
	return out_lo + t * (out_hi - out_lo);
}
#endif //UTILITIES_FUNCTION_H
